package com.javaProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaProject.dto.R;
import com.javaProject.entity.Bishe;
import com.javaProject.service.BisheService;
import com.javaProject.mapper.BisheMapper;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author ASUS
* @description 针对表【bishe】的数据库操作Service实现
* @createDate 2024-03-13 16:00:30
*/
@Service
public class BisheServiceImpl extends ServiceImpl<BisheMapper, Bishe>
    implements BisheService{

    @Override
    public R Delect(Bishe bishe) {

        QueryWrapper<Bishe> query = Wrappers.query(Bishe.class);
        query.eq("bishe_id",bishe.getBisheId());
        Bishe one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("该毕设不存在").setCode(-2);
        }
        if (remove(query)){
            return R.ok().setMsg("删除成功");
        }
        return R.fail().setMsg("信息删除失败");
    }

    @Override
    public List<Bishe> getAllBishe() {
        QueryWrapper<Bishe> query = Wrappers.query(Bishe.class);
        List<Bishe> bishes = list(query);
        return bishes;
    }

    @Override
    public R add(Bishe bishe) {
        QueryWrapper<Bishe> query = Wrappers.query(Bishe.class);
        query.eq("bishe_name",bishe.getBisheName());
        Bishe one = getOne(query);
        if (Objects.nonNull(one)){
            return R.fail().setMsg("该毕设已经存在").setCode(-2);
        }
        Date dateTime = new Date();
        bishe.setCreatetime(dateTime);
        bishe.setIspass("未审核");
        if (save(bishe)){
            return R.ok().setMsg("毕设添加成功").setData(bishe);
        }
        return R.fail().setMsg("毕设添加失败");
    }

    @Override
    public R upData(Bishe bishe) {
        QueryWrapper<Bishe> query = Wrappers.query(Bishe.class);
        query.eq("bishe_id",bishe.getBisheId());
        Bishe one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setData("毕设不存在").setCode(-2);
        }
        if (update(bishe,query)){
            return R.ok().setMsg("毕设信息修成功").setData(bishe);
        }
        return R.fail().setMsg("毕设信息修改失败");
    }

    @Override
    public R upDataIsPass(Bishe bishe) {
        QueryWrapper<Bishe> query= Wrappers.query(Bishe.class);
        query.eq("bishe_id",bishe.getBisheId());
        Bishe one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setData("毕设不存在").setCode(-2);
        }
        one.setIspass(bishe.getIspass());
        if (update(one,query)){

            return R.ok().setMsg("毕设状态修改成功").setData(one);
        }
        return R.fail().setMsg("毕设信息修改失败");
    }

    @Override
    public R upDataIsLock(Bishe bishe) {
        QueryWrapper<Bishe> query= Wrappers.query(Bishe.class);
        query.eq("bishe_id",bishe.getBisheId());
        Bishe one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setData("毕设不存在").setCode(-2);
        }
        one.setIslock(bishe.getIslock());
        if (update(one,query)){
            return R.ok().setMsg("毕设状态修改成功").setData(one);
        }
        return R.fail().setMsg("毕设信息修改失败");
    }

    @Override
    public List<Bishe> getTeacherBishe(Bishe bishe) {
        QueryWrapper<Bishe> query = Wrappers.query(Bishe.class);
        query.eq("teacher_id",bishe.getTeacherId());
        List<Bishe> bishes=list(query);
        return bishes;
    }

    @Override
    public List<Bishe> getIsLockBishe(Bishe bishe) {
        QueryWrapper<Bishe> query = Wrappers.query(Bishe.class);
        query.eq("isPass",bishe.getIspass());
        List<Bishe> bishes=list(query);
        return bishes;
    }

    @Override
    public Bishe getOneBIshe(Integer bishe) {
        QueryWrapper<Bishe> query = Wrappers.query(Bishe.class);
        query.eq("bishe_id",bishe);
        Bishe one = getOne(query);
        return  one;

    }


}




